package com.cskaoyan.service;

import com.cskaoyan.bean.BO.AdminTopicBO;
import com.cskaoyan.bean.PO.MarketCouponUser;
import com.cskaoyan.bean.PO.MarketGoods;
import com.cskaoyan.bean.PO.MarketTopic;
import com.cskaoyan.bean.PO.MarketTopicExample;
import com.cskaoyan.bean.VO.AdminTopicReadVO;
import com.cskaoyan.bean.VO.GoodsListInnerVO;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wenkaikai
 * @since 2022/08/14 15:48
 */
@Service
@Transactional
public class AdminTopicServiceImpl implements AdminTopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;


    /**
     * 专题列表
     *
     * @param info
     * @param title
     * @param subtitle
     * @return
     */
    @Override
    public CommonData<MarketTopic> queryTopicList(BasePageInfo info, String title, String subtitle) {
        MarketTopicExample example = new MarketTopicExample();
        // 都不为空，拼接上order by 条件 进行排序
        if (info.getSort() != null && info.getOrder() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }
        MarketTopicExample.Criteria criteria = example.createCriteria();
        // 不为空，拼接上查询条件,模糊查询
        if (title != null && !"".equals(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (subtitle != null && !"".equals(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        // 查询前开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExampleWithBLOBs(example);
        // 查询结果交给分页工具处理
        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);
        CommonData data = CommonData.data(marketTopicPageInfo);
        return data;
    }

    /**
     * 增加专题,
     * id 靠数据库直接传回
     * sortOrder 不知道是个啥，数据库也没解释
     * addTime 和updateTime 也要加
     * deleted数据库有默认值，可加可不加
     *
     * @param adminTopicBO
     * @return
     */
    @Override
    public AdminTopicBO createTopic(AdminTopicBO adminTopicBO) {
        Date date = new Date();
        adminTopicBO.setAddTime(date);
        adminTopicBO.setUpdateTime(date);
        adminTopicBO.setDeleted(false);
        int code = marketTopicMapper.insertSelective(adminTopicBO);
        if (code == 1) {
            return adminTopicBO;
        }
        return null;
    }

    /**
     * 通过专题id 获取各种各样的数据，回显给前端
     * @param id
     * @return
     */
    @Override
    public AdminTopicReadVO queryTopicById(Integer id) {
        // 首先查询到专题内容封装进对象中
        AdminTopicReadVO adminTopicReadVO = new AdminTopicReadVO();
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        adminTopicReadVO.setTopic(marketTopic);
        // 再找商品信息
        List<GoodsListInnerVO> goodsList = new ArrayList<>();
        Integer[] goods = marketTopic.getGoods();
        if (goods != null && goods.length != 0) {
            // 商品信息不为空时
            for (Integer goodId : goods) {
                MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(goodId);
                GoodsListInnerVO goodsListInnerVO = new GoodsListInnerVO(marketGoods.getId(), marketGoods.getIsHot(), marketGoods.getIsNew(), marketGoods.getName(),
                        marketGoods.getBrief(), marketGoods.getPicUrl(), marketGoods.getRetailPrice(), marketGoods.getCounterPrice());
                goodsList.add(goodsListInnerVO);
            }
        }
        adminTopicReadVO.setGoodsList(goodsList);
        return adminTopicReadVO;
    }

    /**
     * 全参数传，全参数返回
     * sortOrder 不知道是是什么参数，数据库默认值是100
     * 要更新的内容：
     * updateTime
     *
     * @param marketTopic
     * @return
     */
    @Override
    public MarketTopic updateTopic(MarketTopic marketTopic) {
        marketTopic.setUpdateTime(new Date());
        int code = marketTopicMapper.updateByPrimaryKeyWithBLOBs(marketTopic);
        if (code == 1) {
            return marketTopic;
        }
        return null;
    }

    /**
     * 删除专题：
     * 实则通过id修改，修改deleted位为true
     * 更新时间其他不修改
     *
     * @param marketTopic
     * @return
     */
    @Override
    public int deleteTopicLogic(MarketTopic marketTopic) {
        // 用一个新的对象来作为修改目标
        MarketTopic deleteTopics = new MarketTopic();
        deleteTopics.setUpdateTime(new Date());
        deleteTopics.setId(marketTopic.getId());
        deleteTopics.setDeleted(true);
        int code = marketTopicMapper.updateByPrimaryKeySelective(deleteTopics);
        return code;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public int batchDeleteTopics(Integer[] ids) {
        Date date = new Date();
        ArrayList<MarketTopic> deleteTopics = new ArrayList<>();
        for (Integer id : ids) {
            MarketTopic deleteTopic = new MarketTopic();
            deleteTopic.setUpdateTime(date);
            deleteTopic.setId(id);
            deleteTopic.setDeleted(true);
            deleteTopics.add(deleteTopic);
        }
        int count = 0;
        for (MarketTopic deleteTopic : deleteTopics) {
            marketTopicMapper.updateByPrimaryKeySelective(deleteTopic);
            count++;
        }
        return count;
    }
}